﻿@using System.Globalization
@using M2B.GestionEntretiens.Helpers.HTML
@using M2B.GestionEntretiens.POCOS
@model M2B.GestionEntretiens.POCOS.Question

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

<style>
    label {
        margin: 0;
        padding: 0;
        font-size: 14px;
        margin-left: 5px;
        width: 100px;
    }
</style>

<script type="text/javascript">
    
    function CheckBoxIsReponseFormatter(cellvalue, options, rowObject) {
      
        return "<input type='checkbox' id='checkbox"+ options.rowId+"'/>";
    }

   var TrueFalse = "True:False";
    //La liste des propositions de la question
    var propositions;
    //Chargement du document
    $(document).ready(function () {
        LoadPropositions();
    });

    //Chargement des propositions
    function LoadPropositions() {

        var propositionsDiv = document.getElementById("PropositionsDiv");
        propositionsDiv.style.display = "none";
        if ('@Model.Type' == 1) {
            propositionsDiv.style.display = "block";
        }
        LoadPropositionsGrid();


    }

    //Sauvegrade des modifications pour une question
    function SaveAdd(event) {

        var form = $('#formQuestions');
        //Validation des données
        if (form.valid()) {


            var propositions = $("#PropositionsTable").jqGrid('getGridParam', 'data');
            for (var i = 0; i < propositions.length; i++) {
                
                var checkbox = document.getElementById("checkbox" + i);
                if (checkbox != null && checkbox.checked == true) {
                    propositions[i].IsReponse = true;
                } else {
                    propositions[i].IsReponse = false;
                }
            }
            var drpTypeQuestion = document.getElementById("drpTypeQuestion");
            var drpMaxReponse = document.getElementById("drpMaxReponse");
            var drpEvaluation = document.getElementById("drpEvaluation");

            var question = form.serializeObject();
            question.Propositions = propositions;
            question.Type = drpTypeQuestion.options[drpTypeQuestion.selectedIndex].value;
            question.MaxReponses = drpMaxReponse.options[drpMaxReponse.selectedIndex].value;
            question.Evaluation = drpEvaluation.options[drpEvaluation.selectedIndex].value;


            $.ajax({
                url: '@Url.Action("CreateQuestion")',
                type: 'POST',
                data: $.postify(question),
                success: function () {
                    window.location.href = '@Url.Action("Index")';
                }
            });
        }
    }
    function SetResponseProposition(sender) {
       
        var s = sender;
        alert(s);
    }
    //Chargement de la grid propositions
    function LoadPropositionsGrid() {

        jQuery("#PropositionsTable").jqGrid({
            datatype: "local",
            colNames: ["<img style='height:16px;width:16px;' src='../Content/Images/Ajouter.png' onclick='AjouterProposition()'/>", 'Id', 'Proposition', 'Réponse'],
            colModel: [
            { name: 'act', index: 'act', width: 20, sortable: false, search: false },
            { name: 'PropositionId', index: 'PropositionId', width: 40, align: "right", hidden: true },
            { name: 'ContenuProposition', index: 'ContenuProposition', width: 250, align: "left", editable: true },
            { name: 'IsReponse', index: 'IsReponse', width: 70, align: "right", formatter: CheckBoxIsReponseFormatter, formatoptions: { disabled: true } }],
      
            rowNum: 10,
            shrinkToFit: false,
            rowList: [10, 20, 30],
            sortname: 'PropositionId',
            sortorder: "desc",
            viewrecords: true,
            loadonce: true,
            multiselect: false,
            subGrid: false,
            'cellEdit': true,
            'cellsubmit': 'clientArray',
            editurl: 'clientArray',
            caption: "Propositions",
            gridComplete: function () {

                var ids = jQuery("#PropositionsTable").jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var btnSupprimer = "<img style='height:16px;width:16px;' src='../Content/Images/Supprimer.png' onclick='DeleteProposition(" + ids[i] + ")' />";
                    jQuery("#PropositionsTable").jqGrid('setRowData', ids[i], { act: btnSupprimer });
                }
            }
        });

    }
    //Suppression d'une proposition
    function DeleteProposition(propositionIndex) {

        $('#PropositionsTable').delRowData(propositionIndex);

    }
    //Ajout d'un proposition
    function AjouterProposition() {

        propositions = $("#PropositionsTable").jqGrid('getGridParam', 'data');
        var newProposition = { PropositionId: "", ContenuProposition: "La proposition", IsReponse: "" };
        jQuery("#PropositionsTable").addRowData(propositions.length, newProposition);


    }
    function TypeQyestionChanged() {
        var drpTypeQuestion = document.getElementById("drpTypeQuestion");
        var propositionsDiv = document.getElementById("PropositionsDiv");
        propositionsDiv.style.display = "none";
        var typeQuestion = drpTypeQuestion.options[drpTypeQuestion.selectedIndex].value;

        if (typeQuestion != null && typeQuestion == 1) {
            propositionsDiv.style.display = "block";

        }

    }
</script>
@{
    var typeQuestions = (List<Referentiel>)ViewData["TypeQuestions"];
    var types = typeQuestions.Select(item => new SelectListItem { Text = item.Valeur, Value = item.Code, Selected = (Model.Type == item.Code) }).ToList();
    var evaluationQuestions = (List<Referentiel>)ViewData["EvaluationQuestions"];
    var evaluations = evaluationQuestions.Select(item => new SelectListItem { Text = item.Valeur, Value = item.Code, Selected = (Model.Evaluation == item.Code) }).ToList();
    var nbReponsesQuestion = (List<Referentiel>)ViewData["NbReponsesQuestion"];
    var nbReponse = nbReponsesQuestion.Select(item => new SelectListItem { Text = item.Valeur, Value = item.Code, Selected = (Model.MaxReponses.ToString() == item.Code) }).ToList();
}
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "formQuestions" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)

    <fieldset style="border: #C5DBEC solid 1px;">
        <legend>Question</legend>
        @Html.HiddenFor(model => model.QuestionId)
        <table>
            <tr>
                <td>
                    @Html.LabelFor(model => model.ContenuQuestion, "Question")
                </td>
                <td>
                    @Html.TextAreaFor(model => model.ContenuQuestion, new { @class = "TextArea" })
                    @Html.ValidationMessageFor(model => model.ContenuQuestion, "*")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Reponse, "Réponse")
                </td>
                <td>
                    @Html.TextAreaFor(model => model.Reponse, new { @class = "TextArea" })
                    @Html.ValidationMessageFor(model => model.Reponse)
                </td>
            </tr>

            <tr>
                <td>@Html.LabelFor(model => model.Evaluation)</td>
                <td>
                    <div>
                        <table>
                            <tr>
                                <td>
                                    @Html.DropDownList("Value", evaluations, new { @class = "DropDownList", @id = "drpEvaluation" })

                                </td>
                                <td>
                                    @Html.LabelFor(model => model.Type)
                                </td>
                                <td>
                                    @Html.DropDownList("Value", types, new { @class = "DropDownList", @onchange = "TypeQyestionChanged()", @id = "drpTypeQuestion" })

                                </td>

                        </table>
                    </div>
                </td>
            </tr>

            <tr>
                <td>
                    @Html.LabelFor(model => model.MaxReponses, "Nb Rep.")
                </td>
                <td>
                    @Html.DropDownList("Value", nbReponse, new { @class = "DropDownList", @id = "drpMaxReponse" })

                </td>
            </tr>

            <tr>
                <td>
                    @Html.LabelFor(model => model.Commentaire)
                </td>
                <td>
                    @Html.TextAreaFor(model => model.Commentaire, new { @class = "TextArea" })
                    @Html.ValidationMessageFor(model => model.Commentaire)
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <div id="PropositionsDiv" style="display: none;">
                        <table id="PropositionsTable"></table>
                    </div>

                </td>

            </tr>
        </table>



        <div style="float: right">
            <div>
                <table>
                    <tr>
                        <td>
                            @Html.ActionImage("", "", "~/Content/Images/Save.png", "Sauvegarder", "32px", "32px", "SaveAdd(event)")
                            @Html.ActionImage("Index", "", "~/Content/Images/Cancel.png", "Annuler", "32px", "32px", "")
                        </td>
                    </tr>
                </table>
            </div>


        </div>
    </fieldset>

}


